{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-04-22T10:22:59.725456Z",
     "start_time": "2024-04-22T10:22:59.721611Z"
    }
   },
   "source": [
    "import torch\n",
    "\n",
    "# 创建一个二维张量\n",
    "b = torch.tensor([[1, 2, 3], [4, 5, 6]])\n",
    "print(b)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1, 2, 3],\n",
      "        [4, 5, 6]])\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-23T03:11:55.726593Z",
     "start_time": "2024-04-23T03:11:55.723045Z"
    }
   },
   "cell_type": "code",
   "source": [
    "impport torch\n",
    "a=torch.tensor(2,1,64,64)\n",
    "print(a.squeeze(1).shape)\n",
    "print(a.squeeze(2).shape)"
   ],
   "id": "8a779073aea0b5ce",
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (998489127.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001B[0;36m  Cell \u001B[0;32mIn[6], line 1\u001B[0;36m\u001B[0m\n\u001B[0;31m    impport torch\u001B[0m\n\u001B[0m            ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-24T07:33:10.958890Z",
     "start_time": "2024-04-24T07:33:10.954881Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "x=torch.ones(2,2,requires_grad=True)\n",
    "print(x)"
   ],
   "id": "c4cd62cf9ebf478c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1.],\n",
      "        [1., 1.]], requires_grad=True)\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-24T07:35:45.552614Z",
     "start_time": "2024-04-24T07:35:45.547508Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "\n",
    "# 创建一个张量并设置 requires_grad 为 True，以便跟踪其计算历史\n",
    "x = torch.ones(2, 2, requires_grad=True)\n",
    "\n",
    "# 对张量 x 进行操作并创建一个新的张量 y\n",
    "y = x + 2\n",
    "\n",
    "# 对张量 y 进行操作并创建一个新的张量 z\n",
    "z = y * y + 3\n",
    "\n",
    "# 计算张量 z 的均值\n",
    "out = z.mean()\n",
    "\n",
    "# 打印张量 x、y、z 和 out 的值\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)\n",
    "print(out)\n",
    "\n",
    "# 重新计算 out（张量 z 的和）\n",
    "out1 = z.sum()\n",
    "\n",
    "# 打印张量 x 的形状和梯度\n",
    "print(x.shape)\n",
    "print(x.grad)\n",
    "\n",
    "with torch.no_grad():\n",
    "    k = x + 1\n",
    "    print(k)"
   ],
   "id": "d94d64a557554a3b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3., 3.],\n",
      "        [3., 3.]], grad_fn=<AddBackward0>)\n",
      "tensor([[12., 12.],\n",
      "        [12., 12.]], grad_fn=<AddBackward0>)\n",
      "tensor(12., grad_fn=<MeanBackward0>)\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T07:19:43.598355Z",
     "start_time": "2024-04-25T07:19:43.560996Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "\n",
    "class Net(nn.Module):  # 定义了一个名为 Net 的类，它继承自 nn.Module\n",
    "    def __init__(self):  # 构造函数，3x32x32的卷积层，2x2的池化层，64个全连接层\n",
    "        super(Net, self).__init__()  # 调用父类的构造函数\n",
    "\n",
    "        # 定义网络结构\n",
    "        self.conv1 = nn.Conv2d(3,6,3)  # 第一个卷积层，输入通道为3，输出通道为6，卷积核大小为3x3\n",
    "        self.conv2 = nn.Conv2d(6,16,3)  # 第二个卷积层，输入通道为6，输出通道为16，卷积核大小为3x3\n",
    "        self.fc1 = nn.Linear(16*28*28,512)  # 第一个全连接层，输入特征数为16*28*28，输出特征数为512\n",
    "        self.fc2 = nn.Linear(512,64)  # 第二个全连接层，输入特征数为512，输出特征数为64\n",
    "        self.fc3 = nn.Linear(64,10)  # 第三个全连接层，输入特征数为64，输出特征数为10\n",
    "\n",
    "    def forward(self, x):\n",
    "        # 定义前向传播过程\n",
    "        x=self.conv1(x)  # 卷积层 1\n",
    "        x=F.relu(x)  # 使用 ReLU 激活函数\n",
    "\n",
    "        x=self.conv2(x)  # 卷积层 2\n",
    "        x=F.relu(x)  # 使用 ReLU 激活函数\n",
    "\n",
    "        x=x.view(-1,16*28*28)  # 对输入进行展平操作\n",
    "        x=self.fc1(x)  # 全连接层 1\n",
    "        x=F.relu(x)  # 使用 ReLU 激活函数\n",
    "\n",
    "        x=self.fc2(x)  # 全连接层 2\n",
    "        x=F.relu(x)  # 使用 ReLU 激活函数\n",
    "\n",
    "        x=self.fc3(x)  # 全连接层 3\n",
    "        return x  # 返回输出\n",
    "\n",
    "\n",
    "net = Net()  # 创建一个名为net的神经网络实例\n",
    "print(net)  # 打印神经网络实例"
   ],
   "id": "3899071e5b6d9e72",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Net(\n",
      "  (conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1))\n",
      "  (conv2): Conv2d(6, 16, kernel_size=(3, 3), stride=(1, 1))\n",
      "  (fc1): Linear(in_features=12544, out_features=512, bias=True)\n",
      "  (fc2): Linear(in_features=512, out_features=64, bias=True)\n",
      "  (fc3): Linear(in_features=64, out_features=10, bias=True)\n",
      ")\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-24T09:14:17.177616Z",
     "start_time": "2024-04-24T09:14:17.172548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建一个输入数据张量\n",
    "input_data = torch.randn(1, 1, 32, 32)\n",
    "print(input_data)\n",
    "print(input_data.size())"
   ],
   "id": "59a73871a13aee97",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[[-1.1252, -0.1247,  1.8770,  ...,  0.7307, -0.7693, -0.4735],\n",
      "          [ 0.6295, -2.6505,  1.2153,  ..., -0.0652, -0.1730, -0.2521],\n",
      "          [-1.7594,  1.7534,  1.3731,  ...,  1.4611,  1.8640,  0.2884],\n",
      "          ...,\n",
      "          [-0.3907, -0.9796,  1.4657,  ..., -0.2200, -0.0878,  1.1931],\n",
      "          [ 0.0375,  0.4724,  0.0144,  ..., -1.3030, -0.4801, -0.3555],\n",
      "          [-0.8872,  1.1713, -0.6689,  ..., -0.9572,  1.2957, -0.9860]]]])\n",
      "torch.Size([1, 1, 32, 32])\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-24T09:14:30.779858Z",
     "start_time": "2024-04-24T09:14:30.774192Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用神经网络处理输入数据\n",
    "out = net(input_data)\n",
    "print(out)\n",
    "print(out.size())"
   ],
   "id": "cb965783e613cf3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.0419, -0.0475]], grad_fn=<AddmmBackward0>)\n",
      "torch.Size([1, 2])\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-24T09:14:41.058730Z",
     "start_time": "2024-04-24T09:14:41.054383Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建目标张量，并对其进行变形\n",
    "target = torch.randn(2)\n",
    "target = target.view(1, -1)\n",
    "print(target)"
   ],
   "id": "c217c28c1270ca61",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.9226,  1.4304]])\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-24T09:17:49.085483Z",
     "start_time": "2024-04-24T09:17:49.081685Z"
    }
   },
   "cell_type": "code",
   "source": [
    "criterion = nn.L1Loss()  # 使用 L1 损失函数\n",
    "loss = criterion(out, target)  # 计算损失值\n",
    "print(loss) "
   ],
   "id": "679a11015b30a091",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(1.1793, grad_fn=<MeanBackward0>)\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-24T09:36:56.336673Z",
     "start_time": "2024-04-24T09:36:56.301138Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net.zero_grad()  # 清空梯度\n",
    "loss.backward(retain_graph=True)  # 反向传播，并保留计算图\n",
    "\n",
    "import torch.optim as optim\n",
    "optimizer = optim.SGD(net.parameters(), lr=0.01)  # 使用 SGD 优化器,学习速率为0.01\n",
    "optimizer.step()  # 更新参数\n",
    "\n",
    "out=net(input_data)  # 再次使用神经网络处理输入数据\n",
    "print(out)\n",
    "print(out.size())"
   ],
   "id": "74fcc03d1d022759",
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mRuntimeError\u001B[0m                              Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[19], line 2\u001B[0m\n\u001B[1;32m      1\u001B[0m net\u001B[38;5;241m.\u001B[39mzero_grad()  \u001B[38;5;66;03m# 清空梯度\u001B[39;00m\n\u001B[0;32m----> 2\u001B[0m \u001B[43mloss\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mbackward\u001B[49m\u001B[43m(\u001B[49m\u001B[43mretain_graph\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\u001B[43m)\u001B[49m  \u001B[38;5;66;03m# 反向传播，并保留计算图\u001B[39;00m\n\u001B[1;32m      4\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mtorch\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01moptim\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01moptim\u001B[39;00m\n\u001B[1;32m      5\u001B[0m optimizer \u001B[38;5;241m=\u001B[39m optim\u001B[38;5;241m.\u001B[39mSGD(net\u001B[38;5;241m.\u001B[39mparameters(), lr\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m0.01\u001B[39m)  \u001B[38;5;66;03m# 使用 SGD 优化器,学习速率为0.01\u001B[39;00m\n",
      "File \u001B[0;32m/opt/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/_tensor.py:522\u001B[0m, in \u001B[0;36mTensor.backward\u001B[0;34m(self, gradient, retain_graph, create_graph, inputs)\u001B[0m\n\u001B[1;32m    512\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m has_torch_function_unary(\u001B[38;5;28mself\u001B[39m):\n\u001B[1;32m    513\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m handle_torch_function(\n\u001B[1;32m    514\u001B[0m         Tensor\u001B[38;5;241m.\u001B[39mbackward,\n\u001B[1;32m    515\u001B[0m         (\u001B[38;5;28mself\u001B[39m,),\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m    520\u001B[0m         inputs\u001B[38;5;241m=\u001B[39minputs,\n\u001B[1;32m    521\u001B[0m     )\n\u001B[0;32m--> 522\u001B[0m \u001B[43mtorch\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mautograd\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mbackward\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m    523\u001B[0m \u001B[43m    \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mgradient\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mretain_graph\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcreate_graph\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43minputs\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43minputs\u001B[49m\n\u001B[1;32m    524\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[0;32m/opt/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/autograd/__init__.py:266\u001B[0m, in \u001B[0;36mbackward\u001B[0;34m(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)\u001B[0m\n\u001B[1;32m    261\u001B[0m     retain_graph \u001B[38;5;241m=\u001B[39m create_graph\n\u001B[1;32m    263\u001B[0m \u001B[38;5;66;03m# The reason we repeat the same comment below is that\u001B[39;00m\n\u001B[1;32m    264\u001B[0m \u001B[38;5;66;03m# some Python versions print out the first line of a multi-line function\u001B[39;00m\n\u001B[1;32m    265\u001B[0m \u001B[38;5;66;03m# calls in the traceback and some print out the last line\u001B[39;00m\n\u001B[0;32m--> 266\u001B[0m \u001B[43mVariable\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_execution_engine\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mrun_backward\u001B[49m\u001B[43m(\u001B[49m\u001B[43m  \u001B[49m\u001B[38;5;66;43;03m# Calls into the C++ engine to run the backward pass\u001B[39;49;00m\n\u001B[1;32m    267\u001B[0m \u001B[43m    \u001B[49m\u001B[43mtensors\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    268\u001B[0m \u001B[43m    \u001B[49m\u001B[43mgrad_tensors_\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    269\u001B[0m \u001B[43m    \u001B[49m\u001B[43mretain_graph\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    270\u001B[0m \u001B[43m    \u001B[49m\u001B[43mcreate_graph\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    271\u001B[0m \u001B[43m    \u001B[49m\u001B[43minputs\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    272\u001B[0m \u001B[43m    \u001B[49m\u001B[43mallow_unreachable\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[1;32m    273\u001B[0m \u001B[43m    \u001B[49m\u001B[43maccumulate_grad\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[1;32m    274\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n",
      "\u001B[0;31mRuntimeError\u001B[0m: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward."
     ]
    }
   ],
   "execution_count": 19
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
